{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true,
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "datetime是Python处理日期和时间的标准库。\n",
    "\n",
    "### 获取当前日期和时间\n",
    "\n",
    "我们先看如何获取当前日期和时间：\n",
    "\n",
    "```\n",
    ">>> from datetime import datetime\n",
    ">>> now = datetime.now() # 获取当前datetime\n",
    ">>> print(now)\n",
    "2015-05-18 16:28:07.198690\n",
    ">>> print(type(now))\n",
    "<class 'datetime.datetime'>\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "\n",
    "注意到`datetime`是模块，`datetime`模块还包含一个`datetime`类，通过`from datetime import datetime`导入的才是`datetime`这个类。\n",
    "\n",
    "如果仅导入`import datetime`，则必须引用全名`datetime.datetime`。\n",
    "\n",
    "`datetime.now()`返回当前日期和时间，其类型是`datetime`。\n",
    "\n",
    "### 获取指定日期和时间\n",
    "\n",
    "要指定某个日期和时间，我们直接用参数构造一个`datetime`：\n",
    "\n",
    "```\n",
    ">>> from datetime import datetime\n",
    ">>> dt = datetime(2015, 4, 19, 12, 20) # 用指定日期时间创建datetime\n",
    ">>> print(dt)\n",
    "2015-04-19 12:20:00\n",
    "```"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "\n",
    "### datetime转换为timestamp\n",
    "\n",
    "在计算机中，时间实际上是用数字表示的。我们把1970年1月1日 00:00:00 UTC+00:00时区的时刻称为epoch time，记为`0`（1970年以前的时间timestamp为负数），当前时间就是相对于epoch time的秒数，称为timestamp。\n",
    "\n",
    "你可以认为：\n",
    "\n",
    "```\n",
    "timestamp = 0 = 1970-1-1 00:00:00 UTC+0:00\n",
    "```\n",
    "\n",
    "对应的北京时间是：\n",
    "\n",
    "```\n",
    "timestamp = 0 = 1970-1-1 08:00:00 UTC+8:00\n",
    "```\n",
    "\n",
    "可见timestamp的值与时区毫无关系，因为timestamp一旦确定，其UTC时间就确定了，转换到任意时区的时间也是完全确定的，这就是为什么计算机存储的当前时间是以timestamp表示的，因为全球各地的计算机在任意时刻的timestamp都是完全相同的（假定时间已校准）。\n",
    "\n",
    "把一个`datetime`类型转换为timestamp只需要简单调用`timestamp()`方法：\n",
    "\n",
    "```\n",
    ">>> from datetime import datetime\n",
    ">>> dt = datetime(2015, 4, 19, 12, 20) # 用指定日期时间创建datetime\n",
    ">>> dt.timestamp() # 把datetime转换为timestamp\n",
    "1429417200.0\n",
    "```"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "\n",
    "注意Python的timestamp是一个浮点数，整数位表示秒。\n",
    "\n",
    "某些编程语言（如Java和JavaScript）的timestamp使用整数表示毫秒数，这种情况下只需要把timestamp除以1000就得到Python的浮点表示方法。\n",
    "\n",
    "### timestamp转换为datetime\n",
    "\n",
    "要把timestamp转换为`datetime`，使用`datetime`提供的`fromtimestamp()`方法：\n",
    "\n",
    "```\n",
    ">>> from datetime import datetime\n",
    ">>> t = 1429417200.0\n",
    ">>> print(datetime.fromtimestamp(t))\n",
    "2015-04-19 12:20:00\n",
    "```"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "\n",
    "注意到timestamp是一个浮点数，它没有时区的概念，而datetime是有时区的。上述转换是在timestamp和本地时间做转换。\n",
    "\n",
    "本地时间是指当前操作系统设定的时区。例如北京时区是东8区，则本地时间：\n",
    "\n",
    "```\n",
    "2015-04-19 12:20:00\n",
    "```\n",
    "\n",
    "实际上就是UTC+8:00时区的时间：\n",
    "\n",
    "```\n",
    "2015-04-19 12:20:00 UTC+8:00\n",
    "```\n",
    "\n",
    "而此刻的格林威治标准时间与北京时间差了8小时，也就是UTC+0:00时区的时间应该是：\n",
    "\n",
    "```\n",
    "2015-04-19 04:20:00 UTC+0:00\n",
    "```\n",
    "\n",
    "timestamp也可以直接被转换到UTC标准时区的时间：\n",
    "\n",
    "```\n",
    ">>> from datetime import datetime\n",
    ">>> t = 1429417200.0\n",
    ">>> print(datetime.fromtimestamp(t)) # 本地时间\n",
    "2015-04-19 12:20:00\n",
    ">>> print(datetime.utcfromtimestamp(t)) # UTC时间\n",
    "2015-04-19 04:20:00\n",
    "```\n",
    "\n",
    "### str转换为datetime\n",
    "\n",
    "很多时候，用户输入的日期和时间是字符串，要处理日期和时间，首先必须把str转换为datetime。转换方法是通过`datetime.strptime()`实现，需要一个日期和时间的格式化字符串：\n",
    "\n",
    "```\n",
    ">>> from datetime import datetime\n",
    ">>> cday = datetime.strptime('2015-6-1 18:19:59', '%Y-%m-%d %H:%M:%S')\n",
    ">>> print(cday)\n",
    "2015-06-01 18:19:59\n",
    "```"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "\n",
    "字符串`'%Y-%m-%d %H:%M:%S'`规定了日期和时间部分的格式。详细的说明请参考[Python文档](https://docs.python.org/3/library/datetime.html#strftime-strptime-behavior)。\n",
    "\n",
    "注意转换后的datetime是没有时区信息的。\n",
    "\n",
    "### datetime转换为str\n",
    "\n",
    "如果已经有了datetime对象，要把它格式化为字符串显示给用户，就需要转换为str，转换方法是通过`strftime()`实现的，同样需要一个日期和时间的格式化字符串：\n",
    "\n",
    "```\n",
    ">>> from datetime import datetime\n",
    ">>> now = datetime.now()\n",
    ">>> print(now.strftime('%a, %b %d %H:%M'))\n",
    "Mon, May 05 16:28\n",
    "```"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "\n",
    "### datetime加减\n",
    "\n",
    "对日期和时间进行加减实际上就是把datetime往后或往前计算，得到新的datetime。加减可以直接用`+`和`-`运算符，不过需要导入`timedelta`这个类：\n",
    "\n",
    "```\n",
    ">>> from datetime import datetime, timedelta\n",
    ">>> now = datetime.now()\n",
    ">>> now\n",
    "datetime.datetime(2015, 5, 18, 16, 57, 3, 540997)\n",
    ">>> now + timedelta(hours=10)\n",
    "datetime.datetime(2015, 5, 19, 2, 57, 3, 540997)\n",
    ">>> now - timedelta(days=1)\n",
    "datetime.datetime(2015, 5, 17, 16, 57, 3, 540997)\n",
    ">>> now + timedelta(days=2, hours=12)\n",
    "datetime.datetime(2015, 5, 21, 4, 57, 3, 540997)\n",
    "```"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "\n",
    "可见，使用`timedelta`你可以很容易地算出前几天和后几天的时刻。\n",
    "\n",
    "### 本地时间转换为UTC时间\n",
    "\n",
    "本地时间是指系统设定时区的时间，例如北京时间是UTC+8:00时区的时间，而UTC时间指UTC+0:00时区的时间。\n",
    "\n",
    "一个`datetime`类型有一个时区属性`tzinfo`，但是默认为`None`，所以无法区分这个`datetime`到底是哪个时区，除非强行给`datetime`设置一个时区：\n",
    "\n",
    "```\n",
    ">>> from datetime import datetime, timedelta, timezone\n",
    ">>> tz_utc_8 = timezone(timedelta(hours=8)) # 创建时区UTC+8:00\n",
    ">>> now = datetime.now()\n",
    ">>> now\n",
    "datetime.datetime(2015, 5, 18, 17, 2, 10, 871012)\n",
    ">>> dt = now.replace(tzinfo=tz_utc_8) # 强制设置为UTC+8:00\n",
    ">>> dt\n",
    "datetime.datetime(2015, 5, 18, 17, 2, 10, 871012, tzinfo=datetime.timezone(datetime.timedelta(0, 28800)))\n",
    "```"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "\n",
    "如果系统时区恰好是UTC+8:00，那么上述代码就是正确的，否则，不能强制设置为UTC+8:00时区。\n",
    "\n",
    "### 时区转换\n",
    "\n",
    "我们可以先通过`utcnow()`拿到当前的UTC时间，再转换为任意时区的时间：\n",
    "\n",
    "```\n",
    "# 拿到UTC时间，并强制设置时区为UTC+0:00:\n",
    ">>> utc_dt = datetime.utcnow().replace(tzinfo=timezone.utc)\n",
    ">>> print(utc_dt)\n",
    "2015-05-18 09:05:12.377316+00:00\n",
    "# astimezone()将转换时区为北京时间:\n",
    ">>> bj_dt = utc_dt.astimezone(timezone(timedelta(hours=8)))\n",
    ">>> print(bj_dt)\n",
    "2015-05-18 17:05:12.377316+08:00\n",
    "# astimezone()将转换时区为东京时间:\n",
    ">>> tokyo_dt = utc_dt.astimezone(timezone(timedelta(hours=9)))\n",
    ">>> print(tokyo_dt)\n",
    "2015-05-18 18:05:12.377316+09:00\n",
    "# astimezone()将bj_dt转换时区为东京时间:\n",
    ">>> tokyo_dt2 = bj_dt.astimezone(timezone(timedelta(hours=9)))\n",
    ">>> print(tokyo_dt2)\n",
    "2015-05-18 18:05:12.377316+09:00\n",
    "```"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "\n",
    "时区转换的关键在于，拿到一个`datetime`时，要获知其正确的时区，然后强制设置时区，作为基准时间。\n",
    "\n",
    "利用带时区的`datetime`，通过`astimezone()`方法，可以转换到任意时区。\n",
    "\n",
    "注：不是必须从UTC+0:00时区转换到其他时区，任何带时区的`datetime`都可以正确转换，例如上述`bj_dt`到`tokyo_dt`的转换。\n",
    "\n",
    "### 小结\n",
    "\n",
    "`datetime`表示的时间需要时区信息才能确定一个特定的时间，否则只能视为本地时间。\n",
    "\n",
    "如果要存储`datetime`，最佳方法是将其转换为timestamp再存储，因为timestamp的值与时区完全无关。\n",
    "\n",
    "### 练习\n",
    "\n",
    "假设你获取了用户输入的日期和时间如`2015-1-21 9:01:30`，以及一个时区信息如`UTC+5:00`，均是`str`，请编写一个函数将其转换为timestamp："
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "source": [
    "import re\n",
    "from datetime import datetime, timezone, timedelta\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "# 测试:\n",
    "t1 = to_timestamp('2015-6-1 08:10:30', 'UTC+7:00')\n",
    "assert t1 == 1433121030.0, t1\n",
    "\n",
    "t2 = to_timestamp('2015-5-31 16:10:30', 'UTC-09:00')\n",
    "assert t2 == 1433121030.0, t2\n",
    "\n",
    "print('ok')"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "execution_count": 1,
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'to_timestamp' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001B[1;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[1;31mNameError\u001B[0m                                 Traceback (most recent call last)",
      "\u001B[1;32m~\\AppData\\Local\\Temp/ipykernel_73816/3375991791.py\u001B[0m in \u001B[0;36m<module>\u001B[1;34m\u001B[0m\n\u001B[0;32m      8\u001B[0m \u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0;32m      9\u001B[0m \u001B[1;31m# 测试:\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[1;32m---> 10\u001B[1;33m \u001B[0mt1\u001B[0m \u001B[1;33m=\u001B[0m \u001B[0mto_timestamp\u001B[0m\u001B[1;33m(\u001B[0m\u001B[1;34m'2015-6-1 08:10:30'\u001B[0m\u001B[1;33m,\u001B[0m \u001B[1;34m'UTC+7:00'\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0m\u001B[0;32m     11\u001B[0m \u001B[1;32massert\u001B[0m \u001B[0mt1\u001B[0m \u001B[1;33m==\u001B[0m \u001B[1;36m1433121030.0\u001B[0m\u001B[1;33m,\u001B[0m \u001B[0mt1\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0;32m     12\u001B[0m \u001B[1;33m\u001B[0m\u001B[0m\n",
      "\u001B[1;31mNameError\u001B[0m: name 'to_timestamp' is not defined"
     ]
    }
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}